﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;

///Author: NHDuc 
namespace VietThuongLMS.Services
{
    public class AccountService
    {
        public static bool Login(string userName, string passWord)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                int ilogin = context.TaiKhoans.Where(acc => acc.MaTaiKhoan == userName && acc.MatKhau == passWord).Count();
                return (ilogin == 1);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::Login", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool SignIn(string userid, string password)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                return (context.TaiKhoans.Count(a => a.MaTaiKhoan == userid && a.MatKhau == password) == 1);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::SignIn", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public List<TaiKhoan> GetAccountList()
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                List<TaiKhoan> list = context.TaiKhoans.ToList();
                return list;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccountList", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static AccountViewModel GetAccountList(int pageNum, int pageIndex)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                List<TaiKhoan> list = context.TaiKhoans.Include("LoaiTaiKhoan").OrderBy(m => m.NgayDangKy).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = context.TaiKhoans.Count();
                return new AccountViewModel(list, total);

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountService::GetAccountList", ex.StackTrace, ex.Message);
                return new AccountViewModel();
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool InsertAccount(TaiKhoan comp)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                context.AddToTaiKhoans(comp);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::InsertAccount", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static int InsertAccountReturn(TaiKhoan comp)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                context.AddToTaiKhoans(comp);
                context.SaveChanges();
                return comp.ID;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::InsertAccountReturn", ex.StackTrace, ex.Message);
                return -1;
            }
            finally
            {
                context.Dispose();
            }
        }

        //public static bool DeleteAccount(int id)
        //{
        //    try
        //    {
        //        using (LMSDataEntities context = new LMSDataEntities())
        //        {
        //            TaiKhoan obj = context.TaiKhoans.Where(c => c.id == id).FirstOrDefault();
        //            if (obj != null)
        //            {
        //                context.TaiKhoans.DeleteObject(obj);
        //                context.SaveChanges();
        //                return true;
        //            }
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("AccountServices::DeleteAccount", ex.StackTrace, ex.Message);
        //        return false;
        //    }
        //}

        public static bool ChangePass(TaiKhoan acc, string pass)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                TaiKhoan obj = context.TaiKhoans.Where(c => c.MaTaiKhoan == acc.MaTaiKhoan
                                    && c.MatKhau == acc.MatKhau).FirstOrDefault();
                if (obj != null)
                {
                    obj.MatKhau = pass;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::ChangePass", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        public static bool ChangePass(string username, string pass)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                TaiKhoan obj = context.TaiKhoans.Where(c => c.MaTaiKhoan == username).FirstOrDefault();
                if (obj != null)
                {
                    obj.MatKhau = pass;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::ChangePass", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool Edit(TaiKhoan newModel)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                TaiKhoan model = context.TaiKhoans.Where(c => c.ID == newModel.ID).FirstOrDefault();
                if (model != null)
                {
                    model.HoTen = newModel.HoTen;
                    model.DiaChi = newModel.DiaChi;
                    model.DienThoai = newModel.DienThoai;
                    model.Email = newModel.Email;
                    model.GioiTinh = newModel.GioiTinh;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::Edit", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TaiKhoan GetAccountById(int id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                return context.TaiKhoans.Where(c => c.ID == id).FirstOrDefault();

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccountById", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TaiKhoan GetAccountByName(string username)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                return context.TaiKhoans.Where(c => c.MaTaiKhoan == username).FirstOrDefault();

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccountByName", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static int GetAccountIdByName(string username)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                TaiKhoan tk = context.TaiKhoans.Where(c => c.MaTaiKhoan == username).FirstOrDefault();
                if (tk != null)
                    return tk.ID;
                return -1;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccountIdByName", ex.StackTrace, ex.Message);
                return -1;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static bool DeleteAccount(string id)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                TaiKhoan obj = context.TaiKhoans.Where(c => c.MaTaiKhoan == id).FirstOrDefault();
                if (obj != null)
                {
                    //context.TaiKhoans.DeleteObject(obj);
                    obj.Deleted = Utils.Constant.ACTIVE;
                    context.SaveChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::DeleteAccount", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }

        //authenticate
        public static TaiKhoan GetAccount(string userid)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                return context.TaiKhoans.Single(a => a.MaTaiKhoan.Equals(userid));

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccount", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        public static TaiKhoan GetTaiKhoanVaLoai(string userid)
        {
            LMSDataEntities context = null;
            try
            {
                context = new LMSDataEntities();
                return context.TaiKhoans.Include("LoaiTaiKhoan").FirstOrDefault(a => a.MaTaiKhoan.Equals(userid));

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetAccount", ex.StackTrace, ex.Message);
                return null;
            }
            finally
            {
                context.Dispose();
            }
        }

        //reset 
        public static bool ResetPass(TaiKhoan acc)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                TaiKhoan comp = context.TaiKhoans.Where(c => c.ID == acc.ID).FirstOrDefault();
                if (comp != null)
                {
                    comp.MatKhau = acc.MatKhau;
                    context.SaveChanges();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::ResetPass", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        //--K--
        public static AccountViewModel GetTaiKhoanHocVienWithPagination(string kw1, string kw2, string kw3, int pageNum, int pageIndex)
        {
            AccountViewModel acm = new AccountViewModel();
            var context = new LMSDataEntities();
            int k3 = 0;
            if (kw3 != "0")
                k3 = int.Parse(kw3);
            else
                kw3 = "";
            try
            {
                if (kw1 != "" && kw2 != "" && kw3 != "")
                {
                    var tklist = (from tk in context.TaiKhoans
                                  from hv in context.HocViens
                                  from hv_l in context.HocVien_Lop
                                  where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && tk.Lop.Contains(kw2) && tk.Deleted!=1 && hv.Deleted!=1 && hv_l.MaChiNhanh == k3
                                        && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                  select tk).Distinct()
                        .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                    int total = (from tk in context.TaiKhoans
                                 from hv in context.HocViens
                                 from hv_l in context.HocVien_Lop
                                 where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && tk.Lop.Contains(kw2) && tk.Deleted != 1 && hv.Deleted != 1
                                        && hv_l.MaChiNhanh == k3 && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                 select tk).Distinct().Count();
                    acm.List = tklist;
                    acm.Total = total;
                }
                else
                {
                    if (kw1 != "" && kw2 != "")
                    {
                        var tklist = (from tk in context.TaiKhoans
                                      from hv in context.HocViens
                                      from hv_l in context.HocVien_Lop
                                      where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && tk.Lop.Contains(kw2) && tk.Deleted != 1 && hv.Deleted != 1
                                            && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                      select tk).Distinct()
                            .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                        int total = (from tk in context.TaiKhoans
                                     from hv in context.HocViens
                                     from hv_l in context.HocVien_Lop
                                     where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && tk.Lop.Contains(kw2) && tk.Deleted != 1 && hv.Deleted != 1
                                            && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                     select tk).Distinct().Count();
                        acm.List = tklist;
                        acm.Total = total;
                    }
                    if (kw2 != "" && kw3 != "")
                    {
                        var tklist = (from tk in context.TaiKhoans
                                      from hv in context.HocViens
                                      from hv_l in context.HocVien_Lop
                                      where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Lop.Contains(kw2) && hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && hv.Deleted != 1
                                             && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                      select tk).Distinct()
                            .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                        int total = (from tk in context.TaiKhoans
                                     from hv in context.HocViens
                                     from hv_l in context.HocVien_Lop
                                     where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Lop.Contains(kw2) && hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && hv.Deleted != 1
                                            && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                     select tk).Distinct().Count();
                        acm.List = tklist;
                        acm.Total = total;
                    }
                    if (kw1 != "" && kw3 != "")
                    {
                        var tklist = (from tk in context.TaiKhoans
                                      from hv in context.HocViens
                                      from hv_l in context.HocVien_Lop
                                      where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && hv.Deleted != 1
                                            && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                      select tk).Distinct()
                            .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                        int total = (from tk in context.TaiKhoans
                                     from hv in context.HocViens
                                     from hv_l in context.HocVien_Lop
                                     where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && hv.Deleted != 1
                                           && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                     select tk).Distinct().Count();
                        acm.List = tklist;
                        acm.Total = total;
                    }
                    else
                    {
                        if (kw1 != "")
                        {
                            var tklist = (from tk in context.TaiKhoans
                                          where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.HoTen.Contains(kw1) && tk.Deleted != 1 
                                          select tk)
                                .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                            int total = (from tk in context.TaiKhoans
                                         where tk.HoTen.Contains(kw1) && tk.Deleted != 1 
                                         select tk).Count();
                            acm.List = tklist;
                            acm.Total = total;
                        }
                        if (kw2 != "")
                        {
                            var tklist = (from tk in context.TaiKhoans
                                          where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Lop.Contains(kw2) && tk.Deleted != 1 
                                          select tk)
                                .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                            int total = (from tk in context.TaiKhoans
                                         where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Lop.Contains(kw2) && tk.Deleted != 1 
                                         select tk).Count();
                            acm.List = tklist;
                            acm.Total = total;
                        }
                        if (kw3 != "")
                        {
                            int total = (from tk in context.TaiKhoans
                                         from hv in context.HocViens
                                         from hv_l in context.HocVien_Lop
                                         where hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && tk.MaLoaiTaiKhoan.Equals("HocVien") && hv.Deleted != 1
                                                && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                         select tk).Distinct().Count();
                            var tklist = (from tk in context.TaiKhoans
                                          from hv in context.HocViens
                                          from hv_l in context.HocVien_Lop
                                          where hv_l.MaChiNhanh == k3 && tk.Deleted != 1 && tk.MaLoaiTaiKhoan.Equals("HocVien") && hv.Deleted != 1
                                                 && tk.ID == hv.MaTaiKhoan && hv.ID == hv_l.MaHocVien
                                          select tk).Distinct()
                                .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                            acm.List = tklist;
                            acm.Total = total;
                        }
                        if (kw1 == "" && kw2 == "" && kw3 == "")
                        {
                            var tklist = (from tk in context.TaiKhoans
                                          where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Deleted != 1 
                                          select tk)
                                .OrderBy(p => p.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                            int total = (from tk in context.TaiKhoans
                                         where tk.MaLoaiTaiKhoan.Equals("HocVien") && tk.Deleted != 1
                                         select tk).Count();
                            acm.List = tklist;
                            acm.Total = total;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetTaiKhoanByOptionWithPagination", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
            return acm;
        }
        public static AccountViewModel GetTaiKhoanGiaoVienWithPagination(string kw, int pageNum, int pageIndex)
        {
            AccountViewModel acm = new AccountViewModel();
            var context = new LMSDataEntities();
            try
            {
                var tklist = (from tk in context.TaiKhoans
                              from giaovien in context.GiaoViens
                              where tk.MaLoaiTaiKhoan.Equals("GiaoVien") && tk.HoTen.Contains(kw) && tk.Deleted != 1
                                && tk.ID == giaovien.MaTaiKhoan
                              select new TaiKhoanMoRongModel {TK=tk, MaGiaoVien= (int)(from gv in context.GiaoViens
                                                                                  where tk.ID==gv.MaTaiKhoan select gv.ID).FirstOrDefault() })
                    //where tk.HoTen.Contains(kw) select tk)
                             .OrderBy(p => p.TK.HoTen).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = (from tk in context.TaiKhoans
                             from giaovien in context.GiaoViens
                             where tk.MaLoaiTaiKhoan.Equals("GiaoVien") && tk.HoTen.Contains(kw) && tk.Deleted != 1
                                && tk.ID == giaovien.MaTaiKhoan
                             select tk)
                              //where tk.HoTen.Contains(kw) select tk)
                             .OrderBy(p => p.HoTen).Count();
                acm.ListMoRong = tklist;
                acm.TotalMoRong = total;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("AccountServices::GetTaiKhoanGiaoVienWithPagination", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
            return acm;
        }
        //public static IList<HocVien_Lop> GetHocVienLopByID(string IDHocVien)
        //{
        //    var context = new LMSDataEntities();
        //    var hv_lop = (from p in context.HocVien_Lop where p.MaHocVien == IDHocVien select p).ToList();
        //    return hv_lop;
        //}
        //--EOK--
    }
}
